package com.easy.common.util;

import com.easy.common.zhu.LogMethod;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

//打印日志
@Component
@Aspect
public class EasyAspect {
//    @Pointcut("execution(* com.easy.service.impl.*.*(..))")//annotation 注解
//    public void logmethod(){}
    @Around(value="@annotation(logmethod)")
    public Object recordLog(ProceedingJoinPoint point, LogMethod logmethod) throws Throwable {
        Object obj=null;
        StringBuilder strb=new StringBuilder();//记录日志
        String serviceName=logmethod.serviceName();//注解上标记的属性
        boolean decordParam=logmethod.recordREturn();
        MethodSignature methodSignature=(MethodSignature) point.getSignature();//获取method方法签名
        String methodName=methodSignature.getMethod().getName();//获取方法名称
        strb.append("[");
        strb.append(serviceName);//拼接字符串
        strb.append("]");
        strb.append("  method：");
        strb.append(methodName);
        if(decordParam){
            strb.append("params：");
        }

        obj=point.proceed();
        if(decordParam){
            strb.append("  return:  ");
            strb.append(obj);
        }
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss  SSS");
        strb.append(" [");
        strb.append(sdf.format(date));
        strb.append(" ]");
        System.out.println(strb.toString());
        return obj;
    }
}





















